package com.fudan.backend.service.impl;

import com.fudan.backend.dao.AddressDao;
import com.fudan.backend.exception.BaseException;
import com.fudan.backend.exception.RetCode;
import com.fudan.backend.pojo.Address;
import com.fudan.backend.service.AddressService;
import com.fudan.backend.util.Snowflake;
import com.fudan.backend.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    AddressDao addressDao;

    @Autowired
    StringUtil stringUtil;

    @Autowired
    Snowflake snowflake;

    @Override
    public Address getAddressByAddressId(String addressId, String userId) {
        Address address = addressDao.getAddressByAddressId(addressId);
        if (address == null) {
            throw new BaseException(RetCode.ADDRESS_NOT_FOUND);
        }

        if (!address.getUserId().equals(userId)) {
            throw new BaseException(RetCode.UNAUTHORIZED_REQUEST);
        }
        return address;
    }

    @Override
    public Address getDefaultAddress(String userId) {
        Address address = addressDao.getDefaultAddressByUserId(userId);
        if (address == null) {
            throw new BaseException(RetCode.ADDRESS_NOT_FOUND);
        }
        return address;
    }

    @Override
    public List<Address> getAllUserAddress(String userId) {
        List<Address> addList = addressDao.getAllAddressByUserId(userId);
        if (addList.size() != 0) {
            addList.sort(Comparator.comparing(Address::getIsDefault).reversed());
        }
        return addList;
    }

    @Override
    @Transactional
    public RetCode changeDefaultAddress(String addressId, String userId) {
        Address address = addressDao.getAddressByAddressId(addressId);
        if (address == null) {
            throw new BaseException(RetCode.ADDRESS_NOT_FOUND);
        }
        if (!address.getUserId().equals(userId)) {
            throw new BaseException(RetCode.UNAUTHORIZED_REQUEST);
        }
        addressDao.cancelDefaultAddressByUserId(userId);
        addressDao.setDefaultAddressByAddressId(addressId);
        return RetCode.ADDRESS_SUCCESS;
    }

    @Override
    @Transactional
    public RetCode addAddress(Address address, String userId) {
        if (address == null
                || address.getUserId() == null
                || address.getCity() == null
                || address.getName() == null
                || address.getDetail() == null
                || address.getDistrict() == null
                || address.getPhoneNumber() == null
                || address.getProvince() == null
                || address.getIsDefault() == null) {
            throw new BaseException(RetCode.ADDRESS_INFO_NULL);
        }
        //是否本人请求
        if (!address.getUserId().equals(userId)) {
            throw new BaseException(RetCode.UNAUTHORIZED_REQUEST);
        }
        if (!stringUtil.checkPhoneNumberFormat(address.getPhoneNumber())) {
            throw new BaseException(RetCode.PHONE_NUM_FORMAT_ERROR);
        }
        address.setAddressId(snowflake.nextId().toString());
        List<Address> addlist = addressDao.getAllAddressByUserId(userId);
        int isDefault = address.getIsDefault();
        address.setIsDefault(0);
        addressDao.addAddress(address);
        if (addlist.size() > 0 && isDefault == 1) {//该用户已有地址
            this.changeDefaultAddress(address.getAddressId(), userId);
        } else if (addlist.size() == 0) {//第一个地址设为默认地址
            this.changeDefaultAddress(address.getAddressId(), userId);
        }

        return RetCode.ADDRESS_SUCCESS;
    }

    @Override
    @Transactional
    public RetCode deleteAddress(String addressId, String userId) {
        Address address = addressDao.getAddressByAddressId(addressId);
        if (address == null) {
            throw new BaseException(RetCode.ADDRESS_NOT_FOUND);
        }
        if (!address.getUserId().equals(userId)) {
            throw new BaseException(RetCode.UNAUTHORIZED_REQUEST);
        }
        addressDao.deleteAddressByAddressId(addressId);
        //若删除的地址为默认地址，则自动将另一地址设为默认
        if (address.getIsDefault().equals(1)) {
            List<Address> addList = addressDao.getAllAddressByUserId(userId);
            if (addList.size() > 0) {
                addressDao.setDefaultAddressByAddressId(addList.get(0).getAddressId());
            }
        }
        return RetCode.ADDRESS_SUCCESS;
    }

    @Override
    @Transactional
    public RetCode changeAddress(Address address, String userId) {
        Address add = addressDao.getAddressByAddressId(address.getAddressId());
        if (add == null) {
            throw new BaseException(RetCode.ADDRESS_NOT_FOUND);
        }
        if (!(add.getUserId().equals(address.getUserId()) && add.getUserId().equals(userId))) {
            throw new BaseException(RetCode.UNAUTHORIZED_REQUEST);
        }
        if (add.getIsDefault().equals(0) && address.getIsDefault().equals(1)) {
            this.changeDefaultAddress(address.getAddressId(), userId);
        } else if (add.getIsDefault().equals(1) && address.getIsDefault().equals(0)) {
            throw new BaseException(RetCode.ADDRESS_CHANGE_DEFAULT_DENY);
        }
        addressDao.updateAddress(address);
        return RetCode.ADDRESS_SUCCESS;
    }
}
